﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Collections.ObjectModel;
using SMS.Entities.Admin;
using SMS.Common.Silverlight;

namespace SMS.ViewModel.Login
{
    public class ViewModel_Common_Session
    {
        public static UserLoginInformationlist CheckForLoginStatus()
        {
            UserLoginInformationlist objUser = null;
            IsolatedStorageFileStream stream = null;

            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForSite())
            {               
                #region "File Exists"
                if (store.FileExists("Login.dat"))
                {
                    /* Read file data */
                    stream = store.OpenFile("Login.dat", FileMode.Open, FileAccess.Read);
                    var serializer = new DataContractSerializer(typeof(UserInfo));
                    objUser = new UserLoginInformationlist
                    {
                        UserLoginInfo = (UserInfo)serializer.ReadObject(stream)
                    };
                    stream.Close();

                    /* Check for session time (login time) duration */
                    if (Convert.ToDateTime( objUser.UserLoginInfo.LastLogin.ToShortDateString()) == Convert.ToDateTime(DateTime.Now.ToShortDateString())
                        && objUser.UserLoginInfo.LastLogin.AddMinutes(objUser.UserLoginInfo.SessionDuration) >= Convert.ToDateTime(DateTime.Now.ToShortTimeString()))
                    {
                        //
                    }
                    else
                    {
                        // It occurs in this condition when user session expires
                        objUser = null;
                    }
                }
                #endregion
            }

            return objUser;
        }
    }
    public class UserLoginInformationlist
    {
        public UserInfo UserLoginInfo { get; set; }
    }
}
